package com.yz.jdd.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.yz.jdd.R;


public class TopDialog extends Dialog {

    private TextView content;
    private Boolean isCancelable = true;
    private Activity mActivity;

    /**
     * 顶部dialog
     */
    public TopDialog(Context context) {
        super(context, R.style.Cate_Dialog);
        setContentView(R.layout.dialog_permission_top_desc);
        this.mActivity = (Activity) context;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(true);
//        WindowManager.LayoutParams p = getWindow().getAttributes();
//        p.gravity = Gravity.TOP;
//        p.width = WindowManager.LayoutParams.MATCH_PARENT;
//        p.height =  WindowManager.LayoutParams.WRAP_CONTENT;//(int) (d.getWidth() * 0.85);    //宽度设置为屏幕的0.7
//
//        setCancelable(false);
//        getWindow().setAttributes(p);


        // 通过WindowManager获取
        DisplayMetrics dm = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        final WindowManager.LayoutParams params = getWindow().getAttributes();
        params.gravity = Gravity.TOP;
        params.width = dm.widthPixels * 5 / 6;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        setCancelable(false);
        getWindow().setAttributes(params);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }

    public void setContent(String desc) {
        if (!TextUtils.isEmpty(desc)) {
            content = findViewById(R.id.content);
            content.setText(desc);

        }
    }


    @Override
    public void setCancelable(boolean cancelable) {
        isCancelable = cancelable;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!isCancelable) {
                return true;
            }
            this.dismiss();
        }
        return super.onKeyDown(keyCode, event);
    }


}
